import {
	PostBody,
	PostForm, PostKccForm, PostFormWithNoToken, PostBodyWithoutToken
} from './http.js'
// 接口汇总
// 登录
export const login = function(params,callback1,callback2) {
	return PostForm('form/weixin/login', params).then(res => {
		if (res.result == 'true') {
			uni.setStorageSync('session', res.session[0])
			uni.setStorageSync('token', res.TOKEN)
			callback1()
		}else if(res.result == '102'){
			res.data.forEach((item,index)=>{
				res.data[index].check = false
			})
			uni.setStorageSync('list', res.data)
			callback2()
		}else if(res.result == 'false'){
			uni.showModal({
				title:res.msg
			})
		}
	})
}

/*export const loginWithUnionId = function(params,callback1,callback2,callback3,callback4) {
	return PostForm('form/weixin/loginWithUnionId', params).then(res => {
		console.log("登录结果：",res)
		if (res.result == 'true') {
			uni.setStorageSync('session', res.session[0])
			uni.setStorageSync('token', res.TOKEN)
			callback1()
		}else if(res.result == '101'){ //已经有手机号码
			res.data.forEach((item,index)=>{
				res.data[index].check = false
			})
			uni.setStorageSync('list', res.data)
			callback2()
		}else if(res.result == '102'){ //需要获取手机号码
			res.data.forEach((item,index)=>{
				res.data[index].check = false
			})
			uni.setStorageSync('list', res.data)
			callback3()
		}
		else if(res.result == 'false'){
			callback4(res.brand_code,res.invitor_tel)
		}
	})
}*/
//客户登录小程序
export const loginWithUnionId = function(params) {
	return PostForm('form/weixin/loginWithUnionIdV2', params);
}
//师傅登录小程序
export const loginWithMaster = function(params) {
	return PostFormWithNoToken('form/weixin/loginWithMaster', params);
}
//客户直接登录小程序且立马下单
export const loginWithCustomer = function(params) {
	return PostFormWithNoToken('form/weixin/loginWithCustomer', params);
}

//客户H5直接登录且立马下单
export const loginWithCustomerH5 = function(params) {
	return PostFormWithNoToken('form/weixin/loginWithCustomerH5', params);
}

//销售顾问登录小程序
export const loginWithSales = function(params) {
	return PostFormWithNoToken('form/weixin/loginWithSales', params);
}

// 检测是否邀请
export const checkIsAccepte = function(params,callback1,callback2) {
	return PostForm('form/weixin/checkAccept', params).then(res => {
		if (res.result == 'true') {
			callback1()
		} else if(res.result == 'false'){
			callback2()
		}
	})
}

// 检测客串串客户是否注册
export const checkKccIsAccepte = function(params) {
	return PostKccForm('form/weixin/checkAccept', params);
}

// 密码登录
export const loginAuth = function(params,callback1,callback2) {
	return PostForm('/form/auth/login/saas', params).then(res => {
		if (res.result == 'true') {
			uni.setStorageSync('session', res.session[0])
			uni.setStorageSync('token', res.TOKEN)
			callback1()
		}else if(res.result == '102'){
			res.data.forEach((item,index)=>{
				res.data[index].check = false
			})
			uni.setStorageSync('list', res.data)
			callback2()
		}else if(res.result == 'false'){
			uni.showToast({
				title:res.reason,
				icon:'none'
			})
		}
	})
}

// 邀请
export const invite = function(params){

	//return PostForm("form/weixin/acceptInvit",params)
	return PostForm("form/weixin/addCustomer",params)
}

//关注
export const attend = function(params){
	return PostForm("form/weixin/attendInvit",params)
}
// 公开查询方法
export const Proname = function(params){
	return PostForm("form/query",params)
}

//不需token的查询请求
export const PronameWithNoToken = function(params){
	return PostFormWithNoToken("form/query",params)
}

// 客串串列表查询
export const KccProname = function(params){
	return PostKccForm("form/query",params)
}

// 商品类型
export const GoodsType = function(params){
	return PostForm("form/queryTreeProduct",params)
}
// 商家商品类型（未获取手机号码的时候）
export const GoodsBrandType = function(params){
	return PostForm("form/queryBrandTreeProduct",params)
}

// 加入购物车
export const addCart = function(params){
	return PostForm("form/work",params)
}

export const appPay= function(params) {
	return PostKccForm('form/pay/appPay', params);
}


// 获取当前商品的规格 Diy
export const getThisGoodsSkuDiy = function(params){
	return PostForm("form/diy/getMiniSkuGroupData",params)
}
// 获取当前商品的规格 Spu
export const getThisGoodsSkuSpu = function(params){
	return PostForm("spu/getSkuGroupData",params)
}
// 获取多规格 Spu
export const getMiniSkuGroupData = function(params){
	return PostForm("form/spu/getMiniSkuGroupData",params)
}
// 订单完工
export const orderComplete = function(params){
	return PostBodyWithoutToken("form/order/orderComplete",params)
}
// 检查商品是否可退款
export const checkOneCancellation = function(params){
	return PostBodyWithoutToken("form/order/checkOneCancellation",params)
}

// 计算优惠
export const calDiscount = function(params){
	return PostForm("form/order/calDiscount",params)
}

// 订单结算界面数据展示
export const querySettleData = function(params){
	return PostForm("form/order/querySettleData",params)
}

// 订单结算界面数据展示
export const submitOrder = function(params){
	return PostForm("form/order/submitOrder",params)
}

// 立刻提交订单
export const immedSubmitOrder = function(params){
	return PostForm("form/order/immedSubmitOrder",params)
}

//获取我的订单列表
export const queryMyOrderList = function(params) {
	return PostForm('form/order/getMyOrderList', params);
}


// 切换商家
export const switchBusiness = function(params){
	return PostForm("form/weixin/login_without_code",params)
}

// 修改或者添加接口
export const FormWork = function(params){
	return PostForm("form/work",params)
}
//修改购物车内数量
export const updateCartNumApi = function(params){
	return PostBody("form/order/updateCartNum",params)
}
//修改购物车内数量
export const updateOrderProduct = function(params){
	return PostBody("form/order/updateOrderProduct",params)
}
//录入抵扣券
export const addVoucher = function(params){
	return PostBody("/form/order/xsdDeductionlBT",params)
}
//录入抵扣券
export const delVoucher = function(params){
	return PostBody("/form/order/delOneSubsidy",params)
}
//下推配送
export const checkBackReceive = function(params){
	return PostBody("/form/order/checkBackReceive",params)
}
//下推配送
export const checkOrderBack = function(params){
	return PostBody("/form/order/checkOrderBack",params)
}
//下推配送
export const BackReceive = function(params){
	return PostBody("/form/order/BackReceive",params)
}
//删除购物车内数量
export const delOrderProduct = function(params){
	return PostBody("form/order/delOrderProduct",params)
}
//升级为分销商
export const agentApply = function(params){
	return PostBody("/form/order/agentApply",params)
}
//分销商审核
export const agentApplyAudit = function(params){
	return PostBodyWithoutToken("/form/order/agentApplyAudit",params)
}
//取消订单
export const refundFull = function(params){
	return PostBodyWithoutToken("/form/order/refundFull",params)
}
//退款单个
export const refundSingle = function(params){
	return PostBodyWithoutToken("/form/order/orderOneCancellation",params)
}
//商家审核
export const brandApplyAudit = function(params){
	return PostFormWithNoToken("/form/order/brandApplyAudit",params)
}



// 不需token的更新请求
export const FormWorkWithNoToken = function(params){
	return PostFormWithNoToken("form/work",params)
}

export const workBatchSave = function(params){
	return PostBodyWithoutToken("form/workBatchSave?proname=IN6000",params)
}
//确认取消订单
export const ToconfirmCancel = function(params){
	return PostBodyWithoutToken("form/order/checkCancellation",params)
}
//确认取消订单
export const orderCancellation = function(params){
	return PostBodyWithoutToken("form/order/orderCancellation",params)
}
// 不需token的更新请求
export const orderSrvCreate = function(params){
	return PostBody("/form/order/orderSrvCreate",params)
}

// 余额支付
export const payForBalance = function(params){
	return PostForm("form/order/payForBalance",params)
}

// 获取订单列表
export const OrderList = function(params){
	return PostForm("form/query",params)
}

// 商品详细接口。
export const ProductDetails = function(params){
	return PostForm("form/order/getMiniProductDetails",params)
}


// 查询订单详情
export const queryOrderD = function(params){
	return PostForm("form/query",params)
}


// 获取手机号
export const getPhoneNumberApi = function(params){
	return PostForm("form/weixin/getPhoneNumber",params)
}
// 获取手机号
export const getCollectProductData = function(params){
	return PostBody("u8/idhsaas/collectProductData",params)
}


//上传base64图片
export const uploadBase64Str = function(params){
	return PostBody("form/oss/saveToImgByBase64Str",params)
}
//银行的省市区
export const queryProviceBank = function(params){
	return PostKccForm("form/provinceBank",params)
}

//微信支付
export const wx_payment = async (payParam) => {

	let appid="wxd24a8a44250f4264";
	/*#ifdef MP-WEIXIN*/
	const accountInfo = uni.getAccountInfoSync();
	appid = accountInfo.miniProgram.appId;
	/*#endif*/

	console.log('appid', appid);

	if(appid == 'wxd24a8a44250f4264'){
		uni.navigateTo({
			url:'/pages/pay/index_wxmini?brand_name='+payParam.brand_name+'&order_amount='+payParam.order_amount+'&agent_code='+payParam.agent_code+'&back_code='+payParam.back_code+'&target='+payParam.target
				+'&custom_tel='+payParam.custom_tel
		})
	}else{
		let res = await appPay(payParam);
		console.log('调起支付的结果：', res);
		if(res.result == 'ok'){
			uni.navigateToMiniProgram({
				appId: "wxd24a8a44250f4264",
				path: res.wx_path,
				envVersion: 'release',
				success: function (res) {
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/isf/order/order_details?back_code='+payParam.back_code+'&page_src=Share&pay_part_amount='+payParam.order_amount
						})
					}, 1000)
					console.log('调起支付返回的结果：', res);
				},
				fail: function (res) {
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/isf/order/order_details?back_code='+payParam.back_code+'&page_src=Share&pay_part_amount='+payParam.order_amount
						})
					}, 1000)
					console.log('调起支付失败：', res);
				}
			})
		}
	}

}

//更新unionID
export const updateUnionIdReq = function(params){
	return PostForm("form/weixin/updateUnionId",params)
}

//删除订单
export const cancelOrder = function(params){
	return PostFormWithNoToken("form/order/deleteOrder",params)
}

export const getOrderImages = function(params){
	return PostFormWithNoToken("form/query",params)
}

//获取订单所有信息
export const getOrderDetails = function(params){
	return PostFormWithNoToken("form/order/getOrderDetails",params)
}

//获取师傅推荐的商铺
export const getMasterReferShop = function(params){
	return PostForm("form/order/getMasterReferShop",params)
}
//获取客户购买过的商铺
export const getCustomerBuyedBrand = function(params){
	return PostForm("form/order/getCustomerBuyedBrand",params)
}
//实名认证
export const idVerifyApi = function(params){
	return PostFormWithNoToken("form/bank/addUser",params)
}
//查询余额
export const queryBalanceApi = function(params){
	return PostFormWithNoToken("form/bank/queryBalance",params)
}
//绑定银行卡
export const bindCardApi = function(params){
	return PostFormWithNoToken("form/bank/bindCard",params)
}

//交易流水查询
export const queryBankCardApi = function(params){
	return PostFormWithNoToken("form/bank/queryBankCardInfo",params)
}
//交易流水查询
export const queryAcctLogApi = function(params){
	return PostFormWithNoToken("form/bank/queryAcctLog",params)
}
//提现接口
export const cashOutApi = function(params){
	return PostFormWithNoToken("form/bank/cashOut",params)
}
//查询微信订单状态
export const queryWeixinOrderApi = function(params){
	return PostFormWithNoToken("/form/order/queryWeixinOrder",params)
}
